﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using JXC.Models;
using JXC.UI.BaseData;
using JXC.WebApi.EntityExtend;
using SqlSugar.Extensions;

namespace JXC.UI.OutWarehouse
{
    public partial class LSPTJForm : XtraForm
    {
        public List<spxsd> spxsdListR = new List<spxsd>();
        public string code = string.Empty;
        public spxsm data ; 
        private APIClient client=new APIClient();
        public string title = "商品添加(商品销售)";
        public LSPTJForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 增加新商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param> 
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            new ProductEditForm().ShowDialog();
        }

        /// <summary>
        /// 加入所选商品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (gridView1.FocusedRowHandle < 0) return;
            var d = gridView1.GetSelectedRows();
            if (d.Length > 1)
            {
                foreach (var i in d)
                {
                    var s = (spxx)gridView1.GetRow(i);
                    var dd = new spxsd();
                    dd.SPCode = s.SPCode;
                    dd.SPName = s.SPName;
                    dd.DW = s.DW;
                    dd.SL = textEdit2.Text.ObjToDecimal();
                    dd.DJ = s.YSSJ;
                    dd.ZJE = dd.SL * dd.DJ;
                    dd.BZ = s.BZ;
                    dd.GGXH = s.GGXH;
                    spxsdListR.Add(dd);
                    gridControl2.DataSource = null;
                    gridControl2.DataSource = spxsdListR;
                }
            }
            else
            {
                var frm = new SPXXForm();
                frm.data = (spxx)gridView1.GetRow(gridView1.FocusedRowHandle);
                frm.number = textEdit2.Text.ObjToDecimal();

                if (frm.ShowDialog() == DialogResult.OK)
                {
                    spxsdListR.Add(frm.odata);
                    gridControl2.DataSource = null;
                    gridControl2.DataSource = spxsdListR;
                }
            }

            
           
        }

        private void LSPTJForm_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = client.GetSPList();
            this.Text = title;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            var data = (spxx)gridView1.GetRow(gridView1.FocusedRowHandle);
            var form = new SPXXForm();
            form.number = textEdit2.Text.ObjToDecimal();
            form.data=data;

            if (form.ShowDialog() == DialogResult.OK)
            {
                spxsdListR.Add(form.odata);
                gridControl2.DataSource = null;
                gridControl2.DataSource = spxsdListR;
            }
           
        }

        private void gridControl2_DoubleClick(object sender, EventArgs e)
        {
            var data = (spxsd)gridView2.GetRow(gridView2.FocusedRowHandle);
            var form = new SPXXForm();
            form.odata = data;

            if (form.ShowDialog() == DialogResult.OK)
            {
                spxsdListR[gridView2.FocusedRowHandle] = (form.odata);
                gridControl2.DataSource = null;
                gridControl2.DataSource = spxsdListR;
            }
            
        }

        private void simpleButton3_Click(object sender, EventArgs e)
        {
            var data = (spxsd)gridView2.GetRow(gridView2.FocusedRowHandle);
            var form = new SPXXForm();
            form.odata = data;

            if (form.ShowDialog() == DialogResult.OK)
            {
                spxsdListR[gridView2.FocusedRowHandle] = (form.odata);
                gridControl2.DataSource = null;
                gridControl2.DataSource = spxsdListR;
            }
            
        }

        private void simpleButton4_Click(object sender, EventArgs e)
        {
            //var data = (cgrkd)gridView2.GetRow(gridView2.FocusedRowHandle);
            //var form = new SPXXForm();
            //form.odata = data;
            //form.ShowDialog();
            spxsdListR.RemoveAt(gridView2.FocusedRowHandle);;
            gridControl2.DataSource = null;
            gridControl2.DataSource = spxsdListR;
        }

        private void simpleButton5_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(code))
            {
                var data = (List<spxsd>)gridView2.DataSource;
                data.ForEach(p =>
                {
                    p.MCode = code;
                });
                var d = new SPXS() { d = data,m=this.data };
                if (client.AddSPXS(d))
                {

                }
            }
            DialogResult= DialogResult.OK;
            this.Close();
        }

        private void simpleButton6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
